import scrapy
from scrapy_demo.items import BqgItem


class BqgSpider(scrapy.Spider):
    name = 'bqg'
    allowed_domains = ['www.biquger.com']

    def __init__(self, book_id=None, book_name=None, *args, **kwargs):
        super(BqgSpider, self).__init__(*args, **kwargs)
        self.start_urls = ['http://www.biquger.com/biquge/%s/' % book_id]
        self.book_name = book_name

    def parse(self, response):
        items = response.css('#list dd')
        i = 1
        for item in items:
            bqgItem = BqgItem()
            bqgItem['title'] = item.css('a::text').extract_first()
            bqgItem['link'] = item.css('a::attr(href)').extract_first()
            bqgItem['chapter_num'] = i
            i = i + 1
            yield scrapy.Request(bqgItem['link'], meta={'item': bqgItem}, callback=self.detail_parse)

    def detail_parse(self, response):
        item = response.meta['item']
        item['content'] = response.css('.content::text').extract()
        yield item